/**
 * 
 */
package com.agilegroup.crm.ui;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockListActivity;
import com.agilegroup.crm.R;
import com.agilegroup.crm.ui.actions.DialogHandler;

/**
 * @author hisham
 *
 */
public class ListDialogActivity extends SherlockListActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		final String listDialog = intent.getStringExtra("listDialog");
		if(listDialog.equals("1")){
			setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.dialog_list)));
		}
		if(listDialog.equals("2")){
			setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.dialog_two_list)));
		}
		if(listDialog.equals("3")){
			setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.dialog_two_list)));
		}
		final String leadId = intent.getStringExtra("leadId");
		final String email = intent.getStringExtra("email");
		final String mobile = intent.getStringExtra("mobile");
        Log.d("leaId", leadId);
		final ListView listView = getListView();
		listView.setTextFilterEnabled(true);
 
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String selectedValue = (String)listView.getAdapter().getItem(position);
				if(selectedValue.equals("Open")){
					if(listDialog.equals("1")){
						Intent viewLead = new Intent(ListDialogActivity.this,ViewLeadActivity.class);
						viewLead.putExtra("leadId", leadId);
			        	startActivity(viewLead);
			        	finish();
					}
					if(listDialog.equals("2")){
						Intent viewLead = new Intent(ListDialogActivity.this,ViewAccountActivity.class);
						viewLead.putExtra("accountId", leadId);
			        	startActivity(viewLead);
			        	finish();
					}
					if(listDialog.equals("3")){
						Intent viewLead = new Intent(ListDialogActivity.this,ViewContactActivity.class);
						viewLead.putExtra("contactId", leadId);
			        	startActivity(viewLead);
			        	finish();
					}
				}
				if(selectedValue.equals("Add Appointment")){
					Intent newAppointment = new Intent(ListDialogActivity.this,NewAppointmentActivity.class);
					newAppointment.putExtra("leadId", leadId);
					startActivity(newAppointment);
					finish();
				}
				if(selectedValue.equals("Add Call")){
					Intent newCall = new Intent(ListDialogActivity.this,NewCallActivity.class);
					newCall.putExtra("leadId", leadId);
					startActivity(newCall);
					finish();
				}
				if(selectedValue.equals("Call")){
					ArrayList<String>phones = new ArrayList<String>();
					if(mobile!=null&&!mobile.equalsIgnoreCase("")){
						if(mobile.contains(", ")){
							String[]mobileSplit = mobile.split(", ");
							for (int i = 0; i < mobileSplit.length; i++) {
								phones.add(mobileSplit[i]);
							}
						}
						else{
							phones.add(mobile);
						}
					}
					final ArrayAdapter<String>adapter = new ArrayAdapter<String>(ListDialogActivity.this, android.R.layout.select_dialog_item,phones);
					 AlertDialog.Builder builder = new AlertDialog.Builder(ListDialogActivity.this);
					    builder.setTitle("Call");
					    builder.setAdapter(adapter, new OnClickListener() {
							
							@Override
							public void onClick(DialogInterface dialog, int which) {
								DialogHandler.makePhoneCall(adapter.getItem(which), ListDialogActivity.this);
								dialog.dismiss();
								finish();
							}
						});
					    builder.create();
					    builder.show();
				}
				if(selectedValue.equals("Send Message")){
					ArrayList<String>phones = new ArrayList<String>();
					if(mobile!=null&&!mobile.equalsIgnoreCase("")){
						if(mobile.contains(", ")){
							String[]mobileSplit = mobile.split(", ");
							for (int i = 0; i < mobileSplit.length; i++) {
								phones.add(mobileSplit[i]);
							}
						}
						else{
							phones.add(mobile);
						}
					}
					final ArrayAdapter<String>adapter = new ArrayAdapter<String>(ListDialogActivity.this, android.R.layout.select_dialog_item,phones);
					 AlertDialog.Builder builder = new AlertDialog.Builder(ListDialogActivity.this);
					    builder.setTitle("Call");
					    builder.setAdapter(adapter, new OnClickListener() {
							
							@Override
							public void onClick(DialogInterface dialog, int which) {
								DialogHandler.sendMessage(adapter.getItem(which), ListDialogActivity.this);
								dialog.dismiss();
								finish();
							}
						});
					    builder.create();
					    builder.show();
//					if(mobile!=null&&!mobile.equalsIgnoreCase("")){
//						DialogHandler.sendMessage(mobile, ListDialogActivity.this);
//						finish();
//					}
//					else{
//						finish();
//						if(listDialog.equals("1")){
//							Toast.makeText(getApplicationContext(),"There's no Mobile for this lead", Toast.LENGTH_SHORT).show();
//						}
//						if(listDialog.equals("2")){
//							Toast.makeText(getApplicationContext(),"There's no Mobile for this account", Toast.LENGTH_SHORT).show();
//						}
//						if(listDialog.equals("3")){
//							Toast.makeText(getApplicationContext(),"There's no Mobile for this contact", Toast.LENGTH_SHORT).show();
//						}
//					}
				}
				if(selectedValue.equals("Email")){
					if(email!=null&&!email.equalsIgnoreCase("")){
						DialogHandler.sendEmail(email, ListDialogActivity.this);
						finish();
					}
					else{
						finish();
						if(listDialog.equals("1")){
							Toast.makeText(getApplicationContext(),"There's no Email for this lead", Toast.LENGTH_SHORT).show();
						}
						if(listDialog.equals("2")){
							Toast.makeText(getApplicationContext(),"There's no Email for this account", Toast.LENGTH_SHORT).show();
						}
						if(listDialog.equals("2")){
							Toast.makeText(getApplicationContext(),"There's no Email for this contact", Toast.LENGTH_SHORT).show();
						}
					}
				}
			}
		});
	}	
}
